package com.yuanchu.order.controller;

import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.order.dto.OrderDeliveryParamsDto;
import com.yuanchu.order.dto.OrderDetailDto;
import com.yuanchu.order.dto.QueryOrderParamsDto;
import com.yuanchu.order.service.YoshopOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-28 11:30
 */
@Api(value = "订单接口")
@RestController
public class StoreOrderController {

    @Resource
    private YoshopOrderService orderService;
    Integer storeId = 10001;
    @ApiOperation(value = "/order/list&dataType=all&page=1", tags = "查询商家订单列表")
    @GetMapping("/store/list")
    public R geStoreOrderList(QueryOrderParamsDto param) {
        // 根据dataType和page获取用戶订单列表数据
        PageResult<OrderDetailDto> orderList = orderService.queryStoreOrderList(param);
        Map<String, Object> map = new HashMap<>();
        map.put("list", orderList);
        return R.ok().put("data", map);
    }
    
    @ApiOperation(value = "订单发货")
    @PostMapping("/store/delivery")
    public R deliveryOrder(@RequestBody @Validated(ValidationGroups.Update.class) OrderDeliveryParamsDto param) {
        orderService.deliveryOrder(storeId, param);
        return R.ok();
    }
    @ApiOperation("订单删除")
    @PostMapping("/store/delete")
    public R deleteOrder(@RequestBody @Validated(ValidationGroups.Delete.class) OrderDeliveryParamsDto param) {
        boolean deleted = orderService.deleteOrderById(param.getOrderId(), storeId);
        if (!deleted){
            return R.error("订单删除失败");
        }
        return R.ok("订单删除成功");
    }

    @ApiOperation(value = "/order/store/confirmCance", tags = "订单审核")
    @PostMapping("/store/confirmCance")
    public R confirmCanceOrder(@RequestBody OrderDeliveryParamsDto param) {
        Integer orderId = param.getOrderId();
        if (orderId == null){
            return R.error("订单id不能为空");
        }
        boolean confirm = orderService.confirmCancerOrder(orderId,param.getForm().getStatus(), storeId);
        if (!confirm){
            return R.error("订单审核失败");
        }
        return R.ok("订单审核成功");
    }

    // @ApiOperation(value = "/store/order/detail&orderId=3", tags = "根据订单id 查询订单详情")
    // @GetMapping("/store/detail")
    // public R getStoreOrderDetail(@RequestParam Integer orderId) {
    //     OrderDetailDto orderDetail = orderService.getStoreOrderDetail(orderId, storeId);
    //     Map<String, Object> detail = new HashMap<>();
    //     detail.put("detail", orderDetail);
    //     return R.ok().put("data", detail);
    // }

    @ApiOperation(value = "/store/order/detail&orderId=3", tags = "根据订单id 查询订单详情")
    @GetMapping("/store/detail")
    public OrderDetailDto queryStoreOrderDetail(@RequestParam Integer orderId) {
        return orderService.getStoreOrderDetail(orderId, storeId);
    }
}
